﻿using CommonUtility;
using System;
using System.Linq;

namespace WPay.Infrastructure.Unity
{
    public partial class TipResult<T> where T : new()
    {
        public bool Success { get; set; }
        public string Message { get; set; }
        public T Data { get; set; }
    }
    /// <summary>
    /// 全局方法异常捕获
    /// </summary>
    public partial class TipResult
    {
        public bool Success { get; set; }
        public string Message { get; set; }
        public string[] Messages { get; set; }
        public string RedirectUrl { get; set; }
        public string Data { get; set; }

        public TipResult()
        {
            Messages = new string[0];
        }

        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="message"></param>
        public void AddMessage(string message)
        {
            Message = message;
            Messages = Messages.Concat(new[] { message }).ToArray();
        }
        /// <summary>
        /// 添加错误信息
        /// </summary>
        /// <param name="message"></param>
        public void AddErrorMessage(string message)
        {
            Messages = Messages.Concat(new[] { message }).ToArray();
            Message = message;
            Success = false;
        }
        /// <summary>
        /// 添加异常信息
        /// </summary>
        /// <param name="e">The e.</param>
        /// <returns></returns>
        public void AddException(Exception e)
        {
            AddErrorMessage(e.Message);
        }
        /// <summary>
        /// 添加异常信息
        /// </summary>
        /// <param name="e">The e.</param>
        /// <returns></returns>
        public void AddException(UNException e)
        {
            AddErrorMessage(e.Message);
        }
    }
    /// <summary>
    /// 普通API回调,data=回调参数
    /// </summary>
    public partial class APITipResult
    {
        public APITipResult()
        {
            Success = true;
        }
        public int code { get; set; }
        public string Message { get; set; }
        public dynamic data { get; set; }
        public bool Success { get; set; }

        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="message"></param>
        public void AddMessage(string message)
        {
            Message = message;

        }
        /// <summary>
        /// 添加错误信息
        /// </summary>
        /// <param name="message"></param>
        public void AddErrorMessage(string message)
        {

            Message = message;
            Success = false;
        }
        /// <summary>
        /// 添加异常信息
        /// </summary>
        /// <param name="e">The e.</param>
        /// <returns></returns>
        public void AddException(Exception e)
        {
            AddErrorMessage(e.Message);
        }
        /// <summary>
        /// 添加异常信息
        /// </summary>
        /// <param name="e">The e.</param>
        /// <returns></returns>
        public void AddException(UNException e)
        {
            AddErrorMessage(e.Message);
        }
    }


    public static class TipResultExtensions
    {
        public static APITipResult RunWithTry(this APITipResult jsonResultEntry, Action<APITipResult> runMethod)
        {
            try
            {
                runMethod(jsonResultEntry);
            }
            catch (UNException e)
            {
                jsonResultEntry.Success = false;
                jsonResultEntry.AddException(e);
                if (e.InnerException != null)
                {
                    jsonResultEntry.AddException(e.InnerException.InnerException);
                }
                else
                {
                    jsonResultEntry.AddException(new Exception(e.Message));
                }
                LoggerHelper.Log(runMethod.Method.Name, e.GetBaseException().Message);
            }
            catch (Exception e)
            {
                jsonResultEntry.Success = false;
                jsonResultEntry.AddException(e);
                LoggerHelper.Log(runMethod.Method.Name, e.GetBaseException().Message);

            }
            return jsonResultEntry;
        }

    }

}
